package com.hqyj.controller;

import com.hqyj.dao.UserDao;
import com.hqyj.pojo.Orders;
import com.hqyj.pojo.Role;
import com.hqyj.pojo.User;
import com.hqyj.service.RoleService;
import com.hqyj.service.UserService;
import com.hqyj.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chen3
 * @description
 * @date 2021-08-12 16:32
 */
@Controller
public class IndexController {
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public String tologin(){
        return "login/login";
    }

    @RequestMapping("/register")
    public String prepRegister(){
        return "register";
    }

    @RequestMapping("/findByPhone")
    @ResponseBody
    public Map<String,Object> findByPhone(String phone){
        final HashMap<String, Object> map = new HashMap<>();
        final User oneByPhone = userService.getOneByPhone(phone);
        if(oneByPhone!=null){
            map.put("isExisted",true);
        }else {
            map.put("isExisted",false);
        }
        return map;
    }

    @RequestMapping("/findByEmail")
    @ResponseBody
    public Map<String,Object> findByEmail(String email){
        final HashMap<String, Object> map = new HashMap<>();
        final User one = userService.getOnebyEmail(email);
        if(one!=null){
            map.put("isExisted",true);
        }else {
            map.put("isExisted",false);
        }
        return map;
    }

    @RequestMapping("/findByUsername")
    @ResponseBody
    public Map<String,Object> findByUsername(String username){
        final HashMap<String, Object> map = new HashMap<>();
        final User one = userService.getOneByUsername(username);
        if(one!=null){
            map.put("isExisted",true);
        }else {
            map.put("isExisted",false);
        }
        return map;
    }

    @RequestMapping("pay")
    public String pay(String token, HttpSession session){
        try {
            final HashMap<String, Object> map = UserController.getMap().get(token);
            System.out.println(map);
            final User user = new User();
            final User u = (User)map.get("user");
            if(map!=null&&u!=null){
                user.setUsername(u.getUsername());
                user.setBalance((Double) map.get("recharge")+u.getBalance());
                userService.updateUser(user);
                UserController.getMap().remove(token);
                return "pay/ok";
            }else {
                return "pay/fail";
            }
        }catch (Exception e){
            return  "pay/fail";
        }

    }

    @RequestMapping("/forget")
    public String forgetPassword(){
         return "forget";
    }

    @RequestMapping("/find")
    @ResponseBody
    public Map<String,Object> find(User user){
        final HashMap<String, Object> map = new HashMap<>();
        if(user.getEmail()==null||user.getEmail().equals("")){
            System.out.println(user);
            map.put("code",-9);
            map.put("message","参数错误");
        }else {
            final User one = userService.getOneByUsername(user.getUsername());
            if(!one.getEmail().equals(user.getEmail())){
                map.put("code",-1);
                map.put("message","邮箱不匹配");
            }else if(!one.getPhone().equals(user.getPhone())){
                map.put("code",-2);
                map.put("message","电话不匹配");
            }else {
                one.setPassword(MD5Util.md5Hash(user.getPassword(),user.getUsername()));
                try {
                    final int i = userService.updateUser(one);
                    if(i>=1) {
                        map.put("code", 1);
                        map.put("message", "ok");
                    }else {
                        map.put("code", 0);
                        map.put("message", "修改失败");
                    }
                }catch (Exception e){
                    map.put("code", -3);
                    map.put("message", "出错啦");
                }
            }
        }
        return map;
    }
}
